<%@ page import="com.example.util.DBUtil" %>
<%@ page import="com.example.dao.PasswordResetDao" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    // 如果已经登录，跳转到首页
    if (session.getAttribute("user") != null) {
        response.sendRedirect(request.getContextPath() + "/index.jsp");
        return;
    }
    
    // 获取令牌参数
    String token = request.getParameter("token");
    if (token == null || token.trim().isEmpty()) {
        request.setAttribute("error", "无效的重置链接");
    } else {
        try {
            PasswordResetDao passwordResetDao = new PasswordResetDao();
            PasswordResetDao.PasswordResetToken resetToken = passwordResetDao.findToken(token);
            if (resetToken == null) {
                request.setAttribute("error", "重置链接无效或已过期");
            } else {
                // 令牌有效，将其存储在请求属性中供表单使用
                request.setAttribute("token", token);
            }
        } catch (Exception e) {
            request.setAttribute("error", "系统错误: " + e.getMessage());
        }
    }
%>
<!DOCTYPE html>
<html>
<head>
    <title>重置密码</title>
    <link rel="stylesheet" href="<%= request.getContextPath() %>/assets/css/styles.css">
</head>
<body>
    <!-- 导航栏 -->
    <nav class="header-nav">
        <div class="nav-container">
            <a href="<%= request.getContextPath() %>/index.jsp" class="nav-brand">购物网</a>
            
            <ul class="nav-links">
                <li><a href="<%= request.getContextPath() %>/index.jsp">首页</a></li>
                <li><a href="<%= request.getContextPath() %>/products/list.jsp">商品浏览</a></li>
                <li><a href="<%= request.getContextPath() %>/cart/view/cart.jsp">购物车</a></li>
            </ul>
            
            <div class="user-actions">
                <a href="<%= request.getContextPath() %>/auth/login.jsp" class="btn btn-outline">登录</a>
                <a href="<%= request.getContextPath() %>/auth/register.jsp" class="btn" style="margin-left: 10px;">注册</a>
            </div>
        </div>
    </nav>
    
    <div class="container">
        <div class="page-title">
            <h1>重置密码</h1>
        </div>
        
        <% if (request.getAttribute("error") != null) { %>
            <div class="alert alert-error">
                <%= request.getAttribute("error") %>
            </div>
            <div style="text-align: center; margin-top: 1.5rem;">
                <a href="forgot_password.jsp" class="btn">重新申请密码重置</a>
            </div>
        <% } else if (request.getAttribute("success") != null) { %>
            <div class="alert alert-success">
                <%= request.getAttribute("success") %>
            </div>
            <div style="text-align: center; margin-top: 1.5rem;">
                <a href="login.jsp" class="btn">前往登录</a>
            </div>
        <% } else { %>
            <div class="card">
                <div class="card-body">
                    <form action="reset_password_action.jsp" method="post">
                        <input type="hidden" name="token" value="<%= request.getAttribute("token") %>">
                        
                        <div class="form-group">
                            <label for="password">新密码:</label>
                            <input type="password" id="password" name="password" class="form-control" required>
                        </div>
                        
                        <div class="form-group">
                            <label for="confirm_password">确认新密码:</label>
                            <input type="password" id="confirm_password" name="confirm_password" class="form-control" required>
                        </div>
                        
                        <div class="form-group">
                            <input type="submit" value="重置密码" class="btn btn-success" style="width: 100%; padding: 0.75rem; font-size: 1.1rem;">
                        </div>
                    </form>
                </div>
            </div>
        <% } %>
    </div>
</body>
</html>